<?php

namespace app\admin\controller;

use think\Controller;

use Lib\Page;

use think\Request;

class News extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $admin['css'] = ADMIN_CSS;
        $admin['js'] = ADMIN_JS;
        $admin['img'] = ADMIN_IMG;
        $admin['fonts'] = ADMIN_FONTS;
        $admin['vendor'] = ADMIN_VENDOR;
        $this->assign('admin', $admin);
        $this->admin_limit = ADMIN_LIMIT;
        $this->session_admin = session('admin');
        if (!$this->session_admin) {
            controller('login')->login();
            die;
        }
        $this->assign('style', 1);
    }


    /*
     * 资讯分类管理
     */
    public function group()
    {
        $limit = 1;
        $data = controller('data')->group(1, $limit);
        $this->assign('data', $data['data']);
        $this->assign('page', page($data['count'], $limit, 'group'));
        echo $this->fetch("news/group");
    }


    public function group_append()
    {
        $limit = 1;
        $page = input("page");
        $data = controller('data')->group($page, $limit);
        $this->assign('data', $data['data']);

        $return['data'] = $this->fetch("ajax/group_append");
        $return['page'] = page($data['count'], $limit, 'group');
        return $return;
    }

    public function group_add()
    {
        if (Request::instance()->isPost()) {
            $input = input();
            if ($input['title']) {
                $input['addtime'] = time();
                $res = controller('data')->group_add($input);
                if ($res) {
                    $return['status'] = 200;
                    $return['mes'] = "添加成功";
                } else {
                    $return['status'] = 400;
                    $return['mes'] = "系统繁忙";
                }
            } else {
                $return['status'] = 400;
                $return['mes'] = "请输入标题";
            }
            return json($return);
        } else {
            echo $this->fetch("news/group_add");
        }
    }


    public function group_edit()
    {
        if (Request::instance()->isPost()) {
            $input = input();
            if ($input['title']) {
                $input['updatime'] = time();
                $res = controller('data')->group_edit($input);
                if ($res) {
                    $return['status'] = 200;
                    $return['mes'] = "编辑成功";
                } else {
                    $return['status'] = 400;
                    $return['mes'] = "系统繁忙";
                }
            } else {
                $return['status'] = 400;
                $return['mes'] = "请输入标题";
            }
            return json($return);
        } else {
            $id = input("id");
            $data = controller('data')->group_edit($id, 'find');
            $this->assign('data', $data);
            echo $this->fetch("news/group_edit");
        }
    }


    public function group_delete()
    {
        $id = input("id");
        $res = controller('data')->group_delete($id);
        if ($res) {
            $return['status'] = 200;
            $return['mes'] = "删除成功";
        } else {
            $return['status'] = 400;
            $return['mes'] = "系统繁忙";
        }
        return json($return);
    }


    public function index()
    {
        echo $this->fetch("news/index");
    }


    public function add()
    {
        if (Request::instance()->isPost()) {
            $input = input();
            if ($input['title']) {
                $input['inputtime'] = time();
                $res = controller('data')->add($input);
                if ($res) {
                    $return['status'] = 200;
                    $return['mes'] = "添加成功";
                } else {
                    $return['status'] = 400;
                    $return['mes'] = "系统繁忙";
                }
            } else {
                $return['status'] = 400;
                $return['mes'] = "请输入标题";
            }
            return json($return);
        } else {
            $group = controller('data')->group(1, 500000);
            $this->assign('group', $group['data']);
            echo $this->fetch("news/add");
        }
    }

}
